package com.asset.dispoal.business.mediation.service;

import com.asset.dispoal.business.mediation.api.MediationService;
import com.asset.dispoal.business.mediation.dto.CalculateRateRequest;
import com.asset.dispoal.business.mediation.dto.CalculateRateResponse;
import com.asset.dispoal.business.mediation.dto.MediationRequest;
import com.asset.dispoal.business.mediation.dto.MediationResponse;
import com.asset.dispoal.business.mediation.dto.ReduceDtailInfoResponse;
import com.asset.dispoal.business.mediation.dto.ReduceLogsResponse;
import com.asset.dispoal.business.mediation.dto.ReducePresetResponse;
import com.asset.dispoal.business.mediation.dto.SaveReduceInfoRequest;
import com.asset.dispoal.business.mediation.service.common.CalculateRateCommand;
import com.asset.dispoal.business.mediation.service.common.GetOneReduceDetailInfoCommand;
import com.asset.dispoal.business.mediation.service.common.GetReduceDetailCommand;
import com.asset.dispoal.business.mediation.service.common.GetReduceLogsCommand;
import com.asset.dispoal.business.mediation.service.common.QueryAgreementByCaseIdCommand;
import com.asset.dispoal.business.mediation.service.common.QueryBorrowerInfoCommand;
import com.asset.dispoal.business.mediation.service.common.SaveReconcileFileCommand;
import com.asset.dispoal.business.mediation.service.common.SaveReduceInfoCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;
import tech.mhuang.pacebox.springboot.protocol.Result;
import tech.mhuang.pacebox.springboot.protocol.data.PageVO;

import java.util.List;

/**
 * @description:
 * @author: yf
 * @date: 2022-08-23 08:26:58
 */
@Service
public class MediationImpl implements MediationService {

    @Autowired
    private QueryBorrowerInfoCommand queryBorrowerInfoCommand;

    @Autowired
    private QueryAgreementByCaseIdCommand queryAgreementByCaseIdCommand;

    @Autowired
    private SaveReduceInfoCommand saveReduceInfoCommand;

    @Autowired
    private GetReduceLogsCommand getReduceLogsCommand;

    @Autowired
    private GetReduceDetailCommand getReduceRecodeDetailCommand;

    @Autowired
    private CalculateRateCommand calculateRateCommand;

    @Autowired
    private SaveReconcileFileCommand saveReconcileFileCommand;

    @Autowired
    private GetOneReduceDetailInfoCommand getOneReduceDetailInfoCommand;

    @Override
    public Result<PageVO<MediationResponse>> queryBorrowerCaseByPage(MediationRequest mediationRequest) {
        return queryBorrowerInfoCommand.execute(mediationRequest);
    }

    @Override
    public Result<ReducePresetResponse> getAgreeMentCaseListByCaseId(String caseId) {
        return queryAgreementByCaseIdCommand.execute(caseId);
    }

    @Override
    @Transactional
    public Result save(SaveReduceInfoRequest request) {
        //判断和解文件是否存在
        if (!CollectionUtils.isEmpty(request.getReconcileFiles())) {
            saveReconcileFileCommand.execute(request, "reconcileFile");
        }
        return saveReduceInfoCommand.execute(request);
    }

    @Override
    public Result<List<ReduceLogsResponse>> getLogList(String caseId) {
        return getReduceLogsCommand.execute(caseId);
    }

    @Override
    public Result<ReduceDtailInfoResponse> getReduceDetailInfo(String id) {
        return getReduceRecodeDetailCommand.execute(id);
    }

    @Override
    public Result<CalculateRateResponse> calculate(CalculateRateRequest request) {
        return calculateRateCommand.execute(request);
    }

    @Override
    public void reconcile(List<MultipartFile> reconcileFiles, String caseId) {
        //判断和解文件是否存在
        if (!CollectionUtils.isEmpty(reconcileFiles)) {
            SaveReduceInfoRequest request = new SaveReduceInfoRequest();
            request.setCaseId(caseId);
            request.setReconcileFiles(reconcileFiles);
            saveReconcileFileCommand.execute(request, "reconcileFile");
        }
    }

    @Override
    public Result<ReduceDtailInfoResponse> getOneReduceDetail(String caseId) {
        return getOneReduceDetailInfoCommand.execute(caseId);
    }
}
